package com.ruoyi.help.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtils {

    public static void sendMail(String email, String subject, String emailMsg) {
        // 1.创建一个程序与邮件服务器会话对象 Session

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "SMTP");

        //如果是其他邮箱就把qq改为相应的邮箱后缀
        props.setProperty("mail.host", "smtp.qq.com");

        // 指定验证为true
        props.setProperty("mail.smtp.auth", "true");

        // 创建验证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //发邮件的账号和密码，此处需要到QQ邮箱官网开启SMTP,P0P3,IMAP服务，然后获取授权码作为第三方登录密码
                return new PasswordAuthentication("3357124907@qq.com", "pcdintqgufmbchfh");
            }
        };
        //不同与request的session
        Session session = Session.getInstance(props, auth);
        // 2.创建一个Message，它相当于是邮件内容
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress("3357124907@qq.com")); // 设置发送者
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
            message.setSubject(subject);
            // message.setText("这是一封激活邮件，请<a href='#'>点击</a>");
            message.setContent(emailMsg, "text/html;charset=utf-8");
            // 3.创建 Transport用于将邮件发送
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}